package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import entity.Execution;
import entity.Payment;
import util.DBHelper;

public class PaymentDAO {
	private Connection conn = null;
	private Statement st = null;

	public PaymentDAO() {
		try {
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;DatabaseName=Contract", "sa",
					"123456");
			st = conn.createStatement();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public synchronized void release() {
		if (st != null) {
			try {
				st.close();
			} catch (Exception e) {
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}

	}

	public ArrayList<Payment> getAllPayment(String ccompany) {
		ArrayList<Payment> list = new ArrayList<Payment>();
		try {
			ResultSet rs = st.executeQuery("select * from Payment where ccompany='"+ccompany+"';");
			while (rs.next()) {
				Payment pay = new Payment();
				pay.setCno(rs.getString("cno"));
				pay.setPaystate(rs.getString("paystate"));
				pay.setContractp(rs.getString("contractp"));
				pay.setPayprice(rs.getString("payprice"));
				pay.setPaytimes(rs.getString("paytimes"));
				pay.setPrice1(rs.getString("price1"));
				pay.setPrice2(rs.getString("price2"));
				pay.setPrice3(rs.getString("price3"));
				pay.setCcompany(ccompany);
				list.add(pay);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		return list;
	}

	// 链接数据库，从数据库里：根据商品编号获得商品资料
	public Payment getPaymentByCno(String cno) {
		try {
			String sql = "select * from Payment where cno='" + cno + "';"; // SQL语句
			ResultSet rs = st.executeQuery(sql);
			if (rs.next()) {
				Payment pay = new Payment();
				pay.setCno(rs.getString("cno"));
				pay.setPaystate(rs.getString("paystate"));
				pay.setContractp(rs.getString("contractp"));
				pay.setPayprice(rs.getString("payprice"));
				pay.setPaytimes(rs.getString("paytimes"));
				pay.setPrice1(rs.getString("price1"));
				pay.setPrice2(rs.getString("price2"));
				pay.setPrice3(rs.getString("price3"));
				pay.setCcompany(rs.getString("ccompany"));
				return pay;
			} else {
				return null;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public String getPriceByTime(String time,String cno) {
		try {
			String sql = "select * from Payment where cno='" + cno + "';"; // SQL语句
			ResultSet rs = st.executeQuery(sql);
			if (rs.next()) {
				Payment pay = new Payment();
				pay.setCno(rs.getString("cno"));
				pay.setPaystate(rs.getString("paystate"));
				pay.setContractp(rs.getString("contractp"));
				pay.setPayprice(rs.getString("payprice"));
				pay.setPaytimes(rs.getString("paytimes"));
				pay.setPrice1(rs.getString("price1"));
				pay.setPrice2(rs.getString("price2"));
				pay.setPrice3(rs.getString("price3"));
				pay.setCcompany(rs.getString("ccompany"));
				return null;
			} else {
				return null;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	// 获取最近浏览的前五条商品信息
	public ArrayList<Payment> getViewList(String list) {
		System.out.println("list:" + list);
		ArrayList<Payment> executionlist = new ArrayList<Payment>();
		int iCount = 5; // 每次返回前五条记录
		if (list != null && list.length() > 0) {
			String[] arr = list.split(",");
			System.out.println("arr.length=" + arr.length);
			if (arr.length >= 5) {// 如果商品记录大于等于5条
				// (i=8-1;i>=3;i--)
				for (int i = arr.length - 1; i >= arr.length - iCount; i--) {
					executionlist.add(getPaymentByCno(arr[i]));
				}
			} else {// 如果商品小于5条
				for (int i = arr.length - 1; i >= 0; i--) {
					executionlist.add(getPaymentByCno(arr[i]));
				}
			}
			return executionlist;
		} else {
			return null;
		}

	}

	public synchronized void payment_submit(String cno, String payprice, String contractp) throws Exception {
		try {
			Connection conn = null;
			PreparedStatement st = null;
			conn = DBHelper.getConnection();
			int result=Integer.parseInt(contractp)-Integer.parseInt(payprice);
			System.out.println(result);
			
			if (Integer.parseInt(contractp)>Integer.parseInt(payprice)) {
				String sql = "update payment set payprice='" + payprice + "' where cno='" + cno + "'";
				st = conn.prepareStatement(sql);
				ResultSet rs = st.executeQuery();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
